﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dotNetInteropPlugin.PluginDev;
using System.Collections.ObjectModel;
using System.IO;

namespace TestManagedPlugin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           // System.Runtime.InteropServices.Marshal.Copy()
            InitializeComponent();

            //EventHandler f;
            //string PerfTst = null;
            //List<Delegate> NewInvLst = new List<Delegate>();
            //Delegate[] d = f.GetInvocationList();
            //for (int i = 0; i < d.Length; i++)
            //{
            //    try
            //    {
            //        PerfTst = d[i].Target.ToString();
            //        NewInvLst.Add(d[i]);
            //    }
            //    catch
            //    {	
            //    }
            //}

            //f = (EventHandler)Delegate.Combine(NewInvLst.ToArray());
        }
        public IMAIMPPlayer player
        {
            get;set;
        }
        //public SafeEventManager EventDt
        //{
        //    get;set;
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            player.Play();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            player.IsMute = checkBox1.Checked;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            player.Volume = (uint)trackBar1.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = player.ConfigurationManager.ReadParameterString(textBox1.Text, textBox2.Text);
            label2.Text = player.ConfigurationManager.IsSectionExist(textBox1.Text).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label2.Text = player.ConfigurationManager.WriteParameter(textBox1.Text, textBox2.Text, textBox3.Text).ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label2.Text = player.ConfigurationManager.RemoveSection(textBox1.Text).ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox3.Text = player.ConfigurationManager.GetConfigFilePath();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label3.Text = player.LanguageManager.IsSectionExist(textBox6.Text).ToString();
            textBox4.Text = player.LanguageManager.ReadValueString(textBox6.Text, textBox5.Text);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox4.Text = player.LanguageManager.GetCurrentLanguage();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox4.Text = player.LanguageManager.GetLanguageFilePath();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            player.Effects.Speed = (uint)trackBar2.Value;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox7.Text = player.CurrentPlayingInfo.Position.ToString();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            trackBar3.Maximum = (int)player.CurrentPlayingInfo.Length.TotalSeconds;
            try
            {
               // player.AdditionalParams.TrackPosChangedEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_TrackPosChangedEvent); // EventDt.GetInvocation(AdditionalParams_TrackPosChangedEvent);
               // player.AdditionalParams.TrackPosChangedEvent += MAIMPCorssDomainCaller.GetInvocation(AdditionalParams_TrackPosChangedEvent);
                //gggggg = new MAIMPCorssDomainCaller(new MAIMPPlayerEventsDelegate(AdditionalParams_TrackPosChangedEvent));
                //player.AdditionalParams.TrackPosChangedEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_TrackPosChangedEvent);
                //player.AdditionalParams.TrackPosChangedEvent += new MAIMPPlayerEventsDelegate(gggggg.CallFunction);
            }
            catch (Exception rrr)
            {
                MessageBox.Show(rrr.Message);
            }
            
        }
        //MAIMPCorssDomainCaller gggggg = null;
        void AdditionalParams_TrackPosChangedEvent(object sender, MAIMPPlayerEventArgs args)
        {
            trackBar3.Value = (int)player.CurrentPlayingInfo.Position.TotalSeconds;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            player.CurrentPlayingInfo.Position = TimeSpan.FromSeconds(trackBar3.Value);
        }

        private void button11_Click(object sender, EventArgs e)
        {
           // player.PlayingState = AIMPPlayingState.Paused;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox8.Text = player.PlayingOrderParams.IsRepeatTrack.ToString();
            textBox9.Text = player.PlayingOrderParams.OnPlaylistEndAction.ToString();
            textBox10.Text = player.PlayingOrderParams.IsNotRepeatePlaylistWithOneFile.ToString();
            textBox11.Text = player.PlayingOrderParams.IsShuffleMode.ToString();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            player.PlayingOrderParams.IsRepeatTrack = bool.Parse(textBox8.Text);
            //player.PlayingOrderParams.OnPlaylistEndAction = (AIMPOnPlaylistEndAction)uint.Parse(textBox9.Text);
            player.PlayingOrderParams.IsNotRepeatePlaylistWithOneFile = bool.Parse(textBox10.Text);
            player.PlayingOrderParams.IsShuffleMode = bool.Parse(textBox11.Text);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox13.Text = player.AdditionalParams.TimerDisplayStyle.ToString();
            textBox12.Text = player.AdditionalParams.IsOnTopMost.ToString();

            Test ttt = new Test();
            ttt.getVals[1] = 100;

            ReadOnlyCollection<int> f = new ReadOnlyCollection<int>(ttt.getVals);
            
            
        }

        private void button15_Click(object sender, EventArgs e)
        {
            player.AdditionalParams.TimerDisplayStyle = (AIMPTimerDisplayStyle)Enum.Parse(typeof(AIMPTimerDisplayStyle), textBox13.Text);
            player.AdditionalParams.IsOnTopMost = bool.Parse(textBox12.Text);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            checkBox2.Checked = player.Equalizer.Enabled;
            trackBar4.Value = player.Equalizer.Slider31Hz;
            trackBar5.Value = player.Equalizer.Slider16kHz;

            textBox14.Clear();
            
            for (int i = 0; i < player.Equalizer.SlidersCount; i++)
                textBox14.AppendText(player.Equalizer[i].ToString() + "\r\n");
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            player.Equalizer.Slider31Hz = trackBar4.Value;
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            player.Equalizer.Slider16kHz = trackBar5.Value;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            player.Equalizer.Enabled = checkBox2.Checked;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < player.Equalizer.SlidersCount; i++)
            {
                player.Equalizer[i] = int.Parse(textBox14.Lines[i]);
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            MAIMPFileInfo finf = player.CurrentPlayingInfo.GetCurrentTrackInfo();
            listBox1.Items.Clear();
            listBox1.Items.Add(finf.Album);
            listBox1.Items.Add(finf.Artist);
            listBox1.Items.Add(finf.BitRate);
            listBox1.Items.Add(finf.ChanelNumber);
            listBox1.Items.Add(finf.Date);
            listBox1.Items.Add(finf.Duration);
            listBox1.Items.Add(finf.FileName);
            listBox1.Items.Add(finf.FileSize);
            listBox1.Items.Add(finf.Genre);
            listBox1.Items.Add(finf.IsActive);
            listBox1.Items.Add(finf.Rating);
            listBox1.Items.Add(finf.SampleRate);
            listBox1.Items.Add(finf.Title);
            listBox1.Items.Add(finf.TrackID);
        }

        private void button19_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                MAIMPFileInfo finf = player.AdditionalParams.GetTrackInfoFromFile(ofd.FileName);
                listBox1.Items.Clear();
                listBox1.Items.Add(finf.Album);
                listBox1.Items.Add(finf.Artist);
                listBox1.Items.Add(finf.BitRate);
                listBox1.Items.Add(finf.ChanelNumber);
                listBox1.Items.Add(finf.Date);
                listBox1.Items.Add(finf.Duration);
                listBox1.Items.Add(finf.FileName);
                listBox1.Items.Add(finf.FileSize);
                listBox1.Items.Add(finf.Genre);
                listBox1.Items.Add(finf.IsActive);
                listBox1.Items.Add(finf.Rating);
                listBox1.Items.Add(finf.SampleRate);
                listBox1.Items.Add(finf.Title);
                listBox1.Items.Add(finf.TrackID);
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
           // System.Windows.Forms.Application.
            //IPlaylistTest gggg = player as IPlaylistTest;
            //MAIMPConnectedPlaylist tst = gggg.getConPls();

            //tst.LoadTracksFromPlaylist(@"d:\lolo.pls");

          //  tst.PlayTrack(10);

           // tst.SavePlaylist(@"d:\lolo.pls");
           
           // tst.Add(@"g:\Music\UNSORTED\04-Timbaland - Way I are (feat. Keri Hilson and d.o.e.).mp3");

            //tst.Insert(0, @"g:\Music\UNSORTED\04-Timbaland - Way I are (feat. Keri Hilson and d.o.e.).mp3");

           // tst.SelectedTrack = 0;

            //for (int i = 0; i < tst.Count; i++)
            //    listBox1.Items.Add(tst.GetTrackTitle(i));

           // tst.SortByFilter((a, b) => (int)a.BitRate - (int)b.BitRate);

           // tst.AddRange(new DirectoryInfo(@"g:\Music\UNSORTED\").GetFiles().Select(o => o.FullName));
        }

        private void button21_Click(object sender, EventArgs e)
        {
            
        }

        private void button22_Click(object sender, EventArgs e)
        {
            MAIMPConnectedPlaylist tt = player.PlaylistManager[int.Parse(textBox15.Text)];
            listBox2.Items.Clear();
            for (int i = 0; i < tt.Count; i++)
                listBox2.Items.Add(tt.GetTrackTitle(i));
        }

        private void button23_Click(object sender, EventArgs e)
        {
            MAIMPDisconnectedPlaylist dscPls = new MAIMPDisconnectedPlaylist("lolo");
            dscPls.Add(new MAIMPFileInfo() { FileName = @"g:\Music\UNSORTED\04-Timbaland - Way I are (feat. Keri Hilson and d.o.e.).mp3" });
            dscPls.Add(new MAIMPFileInfo() { FileName = @"g:\Music\UNSORTED\05 - MUSIC.mp3", Artist="artist", Title="title" });
            dscPls.Add(player.AdditionalParams.GetTrackInfoFromFile(@"g:\Music\UNSORTED\AC-DC - Shoot to thrill.mp3"));

            player.PlaylistManager.Connect(dscPls, true, false, false);
        }

        private void button24_Click(object sender, EventArgs e)
        {
            MAIMPDisconnectedPlaylist dsc = new MAIMPDisconnectedPlaylist();
            player.PlaylistManager.Disconnect(dsc, player.PlaylistManager["lolo"], false);
            dsc.PlaylistName += "1";
            player.PlaylistManager.Connect(dsc, true, false, false);
        }

        private void button25_Click(object sender, EventArgs e)
        {
            //Size? CurSz1 = player.CoverArtManager.CurrentCoverArtSize;
            //if (CurSz1.HasValue)
            //{
            //    Size CurSz = new Size(pictureBox1.Width, pictureBox1.Height);
            //    player.CoverArtManager.ProportionalSizeCorrection(ref CurSz);
            //    Bitmap Bmp = new Bitmap(CurSz.Width, CurSz.Height);
            //    Graphics gr = Graphics.FromImage(Bmp);
            //    player.CoverArtManager.DrawCurrentCoverArt(gr, new Rectangle(0, 0, Bmp.Width, Bmp.Height));
            //    gr.Dispose();
            //    pictureBox1.Image = Bmp;
            //}

            Size CurSz = new Size(pictureBox1.Width, pictureBox1.Height);
            //Bitmap Bmp = new Bitmap(CurSz.Width, CurSz.Height);
            //Graphics gr = Graphics.FromImage(Bmp);
            Bitmap Bmp = player.CoverArtManager.LoadCoverArtForFile(@"E:\music\Green Day\2004 - American Idiot\Green Day - American Idiot.ape", CurSz);
          //  gr.Dispose();
            pictureBox1.Image = Bmp;

        }

        private void button26_Click(object sender, EventArgs e)
        {
            //player.AdditionalParams.TestFunc();
        }

        private void button27_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
            player.AdditionalParams.GetSupportedExtensions(AIMPExtensionType.PlaylistExts));


            MessageBox.Show(
            player.AdditionalParams.GetSupportedExtensions(AIMPExtensionType.AudioExts));
        }

        private void button28_Click(object sender, EventArgs e)
        {  
            //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
      //      IWin32Window w = player.AdditionalParams.GetAIMPWindow(AIMPPlayerWindow.MainWindow);
        //    Control ff = Control.FromHandle(w.Handle);
            
            //MessageBox.Show("dd");

           // TestSub(player.AdditionalParams.EQChangedEvent, FncFFF);
            
        }

        private bool FncFFF()
        {
            return true;
        }

        public event Func<bool> tstEv;

        private void TestSub(MAIMPPlayerEventsDelegate s, Func<bool> w)
        {

        }

        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }
    }


    class Test
    {
        
        static readonly int[] vals = { 1, 2, 3 };
        
        public int[] getVals
        {
            get { return vals; }
        }
    }
}
